package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sise.bean.CodeMsg;
import com.sise.bean.InitBean;
import com.sise.entity.OrderDetail;
import com.sise.entity.OrderMaster;
import com.sise.entity.ProductInfo;
import com.sise.entity.StoreInfo;
import com.sise.mapper.*;
import com.sise.service.IndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zj
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private OrderMasterMapper orderMasterMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Resource
    private StoreInfoMapper storeInfoMapper;

    @Override
    public CodeMsg init() {
        Integer userCount = userMapper.selectCount(null);
        List<OrderMaster> todayOrder = orderMasterMapper.getTodayOrder();
        Integer productInfoCount = productInfoMapper.selectCount(null);
        InitBean initBean = new InitBean()
                .setUserCount(userCount)
                .setTodayOrderCount(todayOrder.size())
                .setProductInfoCount(productInfoCount);
        int paid;
        int delivered;
        int completed;
        int cancelled;
        paid = delivered = completed = cancelled = 0;
        for (OrderMaster orderMaster : todayOrder) {
            if (orderMaster.getOrderStatus().equals(1)) {
                paid++;
            } else if (orderMaster.getOrderStatus().equals(2)) {
                delivered++;
            } else if (orderMaster.getOrderStatus().equals(3)) {
                completed++;
            } else if (orderMaster.getOrderStatus().equals(4)) {
                cancelled++;
            }
        }
        initBean.setPaid(paid).setDelivered(delivered).setCompleted(completed).setCancelled(cancelled);
        List<Integer> storeIdAll = orderMasterMapper.getStoreIdAll();
        Map<String, Integer[]> storeMap = new HashMap<>();
        Map<String, Integer[]> storeMap2 = new HashMap<>();
        Map<String, Integer> storeMap3 = new HashMap<>();
        Map<String, Integer> storeMap4 = new HashMap<>();
        for (Integer storeId : storeIdAll) {
            StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
            //柱形图
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            int weekday = calendar.get(Calendar.DAY_OF_WEEK);
            String today;
            Integer[] temp = new Integer[weekday];
            for (int i = 0; i < weekday; i++) {
                if (i == 0) {
                    calendar.add(Calendar.DATE, +1);
                } else {
                    calendar.add(Calendar.DATE, 0);
                }
                today = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
                calendar.add(Calendar.DATE, -1);
                String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
                QueryWrapper<OrderMaster> orderMasterQueryWrapper = new QueryWrapper<>();
                orderMasterQueryWrapper.lambda().eq(OrderMaster::getStoreId, storeId).between(OrderMaster::getCreateTime, yesterday, today);
                temp[weekday - i - 1] = orderMasterMapper.selectList(orderMasterQueryWrapper).size();
            }
            storeMap.put(storeInfo.getStoreName(), temp);
            // 折线图
            Integer[] temp2 = new Integer[5];
            // tamp[4]是本月，tamp[3]是上月
            for (int i = 0; i < 5; i++) {
                temp2[4 - i] = orderMasterMapper.getOrdersNByMonthAndStatus(i, 3, storeInfo.getStoreId()).size();
            }
            storeMap2.put(storeInfo.getStoreName(), temp2);
            // 环形图
            Integer ordersYearCount = orderMasterMapper.getOrdersYearStatus(3, storeInfo.getStoreId()).size();
            storeMap4.put(storeInfo.getStoreName(), ordersYearCount);
        }
        //饼状图
        List<OrderMaster> orders = orderMasterMapper.getOrdersNByMonthAndStatus(0, null, null);
        QueryWrapper<OrderDetail> orderDetailQueryWrapper;
        Map<String, Integer> temp = new HashMap<>();
        for (OrderMaster orderMaster : orders) {
            orderDetailQueryWrapper = new QueryWrapper<>();
            orderDetailQueryWrapper.lambda().eq(OrderDetail::getOrderId, orderMaster.getOrderId());
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(orderDetailQueryWrapper);
            for (OrderDetail orderDetail : orderDetails) {
                ProductInfo productInfo = productInfoMapper.selectById(orderDetail.getProductId());
                if (temp.containsKey(productInfo.getProductName())) {
                    temp.put(productInfo.getProductName(),
                            temp.get(productInfo.getProductName()) + orderDetail.getProductBuyNumber());
                } else {
                    temp.put(productInfo.getProductName(), orderDetail.getProductBuyNumber());
                }
            }
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(temp.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        String[] storeMap3key;
        Integer[] storeMap3Value;
        if (list.size() < 7) {
            storeMap3key = new String[list.size()];
            storeMap3Value = new Integer[list.size()];
            for (int i = 0; i < list.size(); i++) {
                storeMap3key[i] = list.get(i).getKey();
                storeMap3Value[i] = list.get(i).getValue();
            }
        } else {
            storeMap3key = new String[7];
            storeMap3Value = new Integer[7];
            for (int i = 0; i < 6; i++) {
                storeMap3key[i] = list.get(i).getKey();
                storeMap3Value[i] = list.get(i).getValue();
            }
            storeMap3key[6] = "其他";
            storeMap3Value[6] = 0;
            for (int i = 6; i < list.size(); i++) {
                storeMap3Value[6] += list.get(i).getValue();
            }
        }
        for (int i = 0; i < storeMap3key.length; i++) {
            storeMap3.put(storeMap3key[i], storeMap3Value[i]);
        }
        initBean.setStoreMap(storeMap);
        initBean.setStoreMap2(storeMap2);
        initBean.setStoreMap3(storeMap3);
        initBean.setStoreMap4(storeMap4);
        return new CodeMsg().setCode(200).setData(initBean);
    }
}
